package adapter;

import android.content.Context;
import android.content.Intent;
import android.net.wifi.ScanResult;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.text.Framgnet.WiFiFragment;
import com.example.administrator.text.HomeActivity;
import com.example.administrator.text.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
import entity.WeWiFiAccount;
import util.BitmapUtil;
import util.LogUtil;
import util.WifiUtil;

/**
 * Created by Administrator on 2016/7/18.
 * 账号中心的适配器
 */
public class RecyclerWeWiFiAccount extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener{
    private List<WeWiFiAccount.BindingInfoBean> mBinDinList;
    private List<ScanResult> mList;
    private JSONArray mJsonArray;
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private OnRecyclerViewItemClickListener mOnItemClickListener;
    private int mChoice,mWiFiOK;  //mChoice 用于区分是什么布局，1账号列表，2代表wifi列表信息 mWiFiOK连接wifi成功  3：首页广告

    public RecyclerWeWiFiAccount(Context context,int choice,List<WeWiFiAccount.BindingInfoBean> binDinList,List<ScanResult> list,JSONArray jsonArray){
        this.mBinDinList = binDinList;
        this.mContext = context;
        this.mChoice = choice;
        this.mList = list;
        this.mJsonArray = jsonArray;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        RecyclerView.ViewHolder holder = null;
        switch (mChoice){
            case 1:
                view = mLayoutInflater.inflate(R.layout.wewifi_recycler_adapter,parent,false);
                holder = new CustomHolder(view);
                view.setOnClickListener(this);
                break;
            case 2:
                view = mLayoutInflater.inflate(R.layout.adapter_wifilist,parent,false);
                holder = new WiFiListHolder(view);
                view.setOnClickListener(this);
                break;
            case 3:
                view = mLayoutInflater.inflate(R.layout.adapter_wifl_welfare,parent,false);
                holder = new welfareHolder(view);
                view.setOnClickListener(this);
                break;
        }
        return holder;
    }

    @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof RecyclerWeWiFiAccount.CustomHolder){
            if(position == mBinDinList.size()-1){
                ((CustomHolder) holder).mTV.setVisibility(View.GONE);
            }
            String name = mBinDinList.get(position).getAccount();
            if(((CustomHolder)holder).mTVPhone !=null){
                ((CustomHolder)holder).mTVPhone.setText(name);
            }
            if(((CustomHolder)holder).mTVName !=null){
                ((CustomHolder)holder).mTVName.setText(mBinDinList.get(position).getName());
            }
            if(((CustomHolder)holder).mImageView !=null){
                BitmapUtil.newBitmap().showBitmap(mContext,mBinDinList.get(position).getPic(),((CustomHolder)holder).mImageView,R.mipmap.homepage_icon,null);
            }
            holder.itemView.setTag(position);

        }else if(holder instanceof WiFiListHolder){
            if(mList.size()>0){
                if(((WiFiListHolder) holder).mTVName !=null && mList.get(position).SSID != null){
                    ((WiFiListHolder) holder).mTVName.setText(mList.get(position).SSID);
                }
                int level = mList.get(position).level;
                if (level <= 0 && level >= -50) {
                    if(((WiFiListHolder) holder).mIVDegree != null){
                        ((WiFiListHolder) holder).mIVDegree.setBackgroundResource(R.mipmap.wifi1_blue_45);
                    }
                } else if (level < -50 && level >= -75) {
                    if(((WiFiListHolder) holder).mIVDegree != null){
                        ((WiFiListHolder) holder).mIVDegree.setBackgroundResource(R.mipmap.wifi2_blue_39);
                    }
                } else if (level < -75 && level >= -85) {
                    if(((WiFiListHolder) holder).mIVDegree != null){
                        ((WiFiListHolder) holder).mIVDegree.setBackgroundResource(R.mipmap.wifi3_blue_85);
                    }
                } else if (level < -85 && level >= -100) {
                    if(((WiFiListHolder) holder).mIVDegree != null){
                        ((WiFiListHolder) holder).mIVDegree.setBackgroundResource(R.mipmap.wifi4_blue_79);
                    }
                } else {
                    if(((WiFiListHolder) holder).mIVDegree != null){
                        ((WiFiListHolder) holder).mIVDegree.setBackgroundResource(R.mipmap.wifi5);
                    }
                }

                if(mList.get(position).SSID.equals(WifiUtil.getInstance(mContext).getWIFIName())){
                    if( ((WiFiListHolder) holder).mIVConnect !=null){
                        ((WiFiListHolder) holder).mIVConnect.setVisibility(View.VISIBLE);
                    }
                    mWiFiOK = position;
                    if(mList.get(position).SSID.equals(WiFiFragment.mNewFiWiFi)){
                        if (level <= 0 && level >= -50) {
                            if(((WiFiListHolder) holder).mIVDegree !=null){
                                ((WiFiListHolder) holder).mIVDegree.setBackgroundResource(R.mipmap.newfi1);
                            }
                        } else if (level < -50 && level >= -100) {
                            if(((WiFiListHolder) holder).mIVDegree !=null){
                                ((WiFiListHolder) holder).mIVDegree.setBackgroundResource(R.mipmap.newfi2);
                            }
                        }
                    }
                }else {
                    if(((WiFiListHolder) holder).mIVConnect !=null){
                        ((WiFiListHolder) holder).mIVConnect.setVisibility(View.GONE);
                    }
                }
            }
        }else if(holder instanceof welfareHolder){
            try {
                if(((welfareHolder)holder).mIVba !=null){
                    holder.itemView.setTag(position);
                    JSONObject jsonObject = mJsonArray.getJSONObject(position);
                    LogUtil.eE("WiFiUrl",jsonObject.getString("thumbnail"));
                    BitmapUtil.newBitmap().showBitmap(mContext,jsonObject.getString("thumbnail"),((welfareHolder)holder).mIVba,R.mipmap.pre_home_head,null);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    @Override public int getItemCount() {
        if(mChoice == 1){
            return mBinDinList.size();
        }else if(mChoice == 2){
            return mList.size();
        }else if(mChoice == 3){
            return mJsonArray.length();
        }
        return 0;
    }

    @Override public void onClick(View v) {
        if(v !=null && v.getTag() !=null){
            int teg = (int) v.getTag();
            if(mChoice == 1){
                LogUtil.eE("","mOnItemClickListener"+mOnItemClickListener+"......v....."+v+"v.getTag()"+ teg);
                if(mOnItemClickListener !=null){
                    mOnItemClickListener.onItemClick(v,teg);
                }
            }else if(mChoice == 3){
                Intent intent = new Intent(HomeActivity.mAction);
                intent.putExtra("wifi","fuliOnClick");
                mContext.sendBroadcast(intent);
            }
        }
    }

     static class CustomHolder extends RecyclerView.ViewHolder{
     @Bind(R.id.iamge_wewifiadapter)         ImageView mImageView;    //图片
     @Bind(R.id.text_wewifiadapter_name)     TextView mTVName;       //Namw
     @Bind(R.id.text_wewifiadapter_phone)    TextView mTVPhone;     //Namw
     @Bind(R.id.tv_wewifl)                   TextView mTV;     //Namw
         CustomHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

     static class WiFiListHolder extends RecyclerView.ViewHolder{
        @Bind(R.id.adapter_wifilist_iv_degree)  ImageView mIVDegree;    //图片
        @Bind(R.id.adapter_wifilist_iv_connect) ImageView mIVConnect;    //图片
        @Bind(R.id.adapter_wifilist_tv_name)    TextView mTVName;       //Namw

         WiFiListHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
     }

    static class welfareHolder extends RecyclerView.ViewHolder{

        @Bind(R.id.im_adapter_wifi_welfare)  ImageView mIVba;    //图片
        welfareHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * 定义接口 用于itme  的回调
     */
    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view,int position);
    }
    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    /**
     * wifi 切换后改变部分UI
     */
    public void showView(List<ScanResult> list){
        mList = list;
    }
}
